package com.aysafety.core.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.alibaba.fastjson.JSON;

public class CacheUtils {

    private static final Logger logger = LoggerFactory.getLogger(CacheUtils.class);

    private static final String cacheKeyPrefix = "iotDataPlatform:cache:";

    public static final long cacheTTLDefault = 600;

    public static final long cacheTTLMin = 60;

    public static final long cacheTTLClockDefault = 3600;

    public static final long cacheTTLMax = 86400;

    public static boolean exists(StringRedisTemplate stringRedisTemplate, String key) {
        key = cacheKeyPrefix + key;
        if (stringRedisTemplate.opsForValue().size(key) > 0) {
            return true;
        }
        return false;
    }

    public static boolean delete(StringRedisTemplate stringRedisTemplate, String key) {
        key = cacheKeyPrefix + key;
        stringRedisTemplate.delete(key);
        return true;
    }

    public static boolean set(StringRedisTemplate stringRedisTemplate, String key, Map<String, Object> map, long ttl) {
        key = cacheKeyPrefix + key;
        stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(map), ttl, TimeUnit.SECONDS);
        return true;
    }

    public static Map<String, Object> getMap(StringRedisTemplate stringRedisTemplate, String key) {
        key = cacheKeyPrefix + key;
        Map<String, Object> data = new HashMap<String, Object>();
        if (stringRedisTemplate.opsForValue().size(key) > 0) {
            String dataString = stringRedisTemplate.opsForValue().get(key);
            data = JSON.parseObject(dataString, Map.class);
        }
        return data;
    }

    public static boolean set(StringRedisTemplate stringRedisTemplate, String key, List<Map<String, Object>> lists,
        long ttl) {
        key = cacheKeyPrefix + key;
        stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(lists), ttl, TimeUnit.SECONDS);
        return true;
    }

    public static List<Map<String, Object>> getList(StringRedisTemplate stringRedisTemplate, String key) {
        key = cacheKeyPrefix + key;
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        if (stringRedisTemplate.opsForValue().size(key) > 0) {
            String dataString = stringRedisTemplate.opsForValue().get(key);
            data = JSON.parseObject(dataString, List.class);
        }
        return data;
    }

    public static boolean set(StringRedisTemplate stringRedisTemplate, String key, String value, long ttl) {
        key = cacheKeyPrefix + key;
        stringRedisTemplate.opsForValue().set(key, value, ttl, TimeUnit.SECONDS);
        return true;
    }

    public static String get(StringRedisTemplate stringRedisTemplate, String key) {
        key = cacheKeyPrefix + key;
        String dataString = "";
        if (stringRedisTemplate.opsForValue().size(key) > 0) {
            dataString = stringRedisTemplate.opsForValue().get(key);
        }
        return dataString;
    }

    public static boolean set(StringRedisTemplate stringRedisTemplate, String key, long value, long ttl) {
        key = cacheKeyPrefix + key;
        stringRedisTemplate.opsForValue().set(key, String.valueOf(value), ttl, TimeUnit.SECONDS);
        return true;
    }

    public static long getLong(StringRedisTemplate stringRedisTemplate, String key) {
        key = cacheKeyPrefix + key;
        long data = Long.MIN_VALUE;
        if (stringRedisTemplate.opsForValue().size(key) > 0) {
            String dataString = stringRedisTemplate.opsForValue().get(key);
            data = Long.parseLong(dataString);
        }
        return data;
    }

    public static boolean set(StringRedisTemplate stringRedisTemplate, String key, int value, long ttl) {
        key = cacheKeyPrefix + key;
        stringRedisTemplate.opsForValue().set(key, String.valueOf(value), ttl, TimeUnit.SECONDS);
        return true;
    }

    public static int getInt(StringRedisTemplate stringRedisTemplate, String key) {
        key = cacheKeyPrefix + key;
        int data = Integer.MIN_VALUE;
        if (stringRedisTemplate.opsForValue().size(key) > 0) {
            String dataString = stringRedisTemplate.opsForValue().get(key);
            data = Integer.parseInt(dataString);
        }
        return data;
    }

    public static boolean set(StringRedisTemplate stringRedisTemplate, String key, double value, long ttl) {
        key = cacheKeyPrefix + key;
        stringRedisTemplate.opsForValue().set(key, String.valueOf(value), ttl, TimeUnit.SECONDS);
        return true;
    }

    public static double getDouble(StringRedisTemplate stringRedisTemplate, String key) {
        key = cacheKeyPrefix + key;
        double data = Double.MIN_VALUE;
        if (stringRedisTemplate.opsForValue().size(key) > 0) {
            String dataString = stringRedisTemplate.opsForValue().get(key);
            data = Double.parseDouble(dataString);
        }
        return data;
    }

    public static boolean set(StringRedisTemplate stringRedisTemplate, String key, float value, long ttl) {
        key = cacheKeyPrefix + key;
        stringRedisTemplate.opsForValue().set(key, String.valueOf(value), ttl, TimeUnit.SECONDS);
        return true;
    }

    public static float getFloat(StringRedisTemplate stringRedisTemplate, String key) {
        key = cacheKeyPrefix + key;
        float data = Float.MIN_VALUE;
        if (stringRedisTemplate.opsForValue().size(key) > 0) {
            String dataString = stringRedisTemplate.opsForValue().get(key);
            data = Float.parseFloat(dataString);
        }
        return data;
    }
}
